<template>
	<view :class="darkMode ? 'custom-dark' : 'custom-light'" class="w-h-100">
		<!-- <button style="margin:20rpx 60rpx" type="primary" @click="uploadPreviewFile">H5上传文件，云预览</button>
		<button style="margin:20rpx 60rpx" type="primary" @click="uploadEditFile">H5上传文件，云编辑</button>
		<button style="margin:20rpx 60rpx" type="primary" @click="uploadEditFileHttp">HTTP上传文件，云编辑</button>
		<button style="margin:20rpx 60rpx" type="primary" @click="uploadFormatConvertFile">H5上传文件，格式转换</button> -->

		<!-- <div style="margin:20rpx 60rpx;font-size:40rpx;font-weight:500">条码</div>
		<span v-if="scanCodeResult" style="margin:20rpx 60rpx;font-size:64rpx;font-weight:700">{{ scanCodeResult }}</span>
		<div style="margin:20rpx 60rpx;font-size:40rpx;font-weight:500">航班信息</div> -->
		<view class="mycard" v-if="hanbanList && hanbanList.length > 0">
			<uni-card>
				<view v-for="(item, index) in hanbanList" :key="index" class="card-item audit-card-content">
					<!-- <view class="uni-flex uni-row">
						<view class="flex-item-20">{{item.title}}</view>
						<view class="flex-item-80">{{item.content}}</view>
					</view> -->
					<view style="font-size: 48rpx;color:#6D6D72">{{item.title}}</view>
					<view style="font-size: 64rpx;font-weight: 700;color:#161A23;text-align: right;">{{item.content}}</view>
				</view>
			</uni-card>
		</view>
		<button style="position:absolute;bottom:40rpx;width:80%;right:0;left:0" type="primary" @click="handleScanCode">扫码</button>
	</view>
</template>

<script>
import { mapGetters } from 'vuex'
import globalConfig from '@/config'
import { generateSign } from './signclient.js'
export default {
	computed: {
		...mapGetters(['themeBgColor', 'darkMode'])
	},
	data() {
		return {
			// 条码内容
			scanCodeResult: '',
			// 航班信息列表
			hanbanList: []
		}
	},
	onReady() {
		uni.setNavigationBarTitle({
			// title: this.$t('YzCloud'),
			title: '扫码'
		})
		this.setNavBarColor()
	},
	onShow() {
		this.setNavBarColor()
	},
	methods: {
		// 扫码：二维码，条形码等
		handleScanCode() {
			// #ifdef H5
			uni.showToast({
				title: 'H5端不能扫码，请使用手机端扫码'
			})
			// #endif
			// #ifndef H5
			// 允许从相机和相册扫码
			uni.scanCode({
				success: res => {
					const { scanType, result } = res
					console.log('条码类型：' + scanType)
					console.log('条码内容：' + result)
					switch (scanType) {
						case 'EAN_13':
							this.scanCodeResult += '条形码：' + result + '\n'
							break
						case 'QR_CODE':
							this.scanCodeResult += '二维码：' + result + '\n'
							break
						case 'BAR_CODE':
							this.scanCodeResult += '一维码：' + result + '\n'
							break
						case 'DATAMATRIX':
							this.scanCodeResult += 'Data Matrix码：' + result + '\n'
							break
						case 'PDF417':
							this.scanCodeResult += 'PDF417条码：' + result + '\n'
							break
						default:
							this.scanCodeResult += result + '\n'
							break
					}
					// this.handleScanCode()
					// 扫码结果上传
					this.scanCodeUpload(result)
				}
			})
			// #endif
		},
		// 扫码结果上传
		scanCodeUpload(strBarcode) {
			this.$minApi.scanCodeUpload(strBarcode).then(res => {
				this.hanbanList = res.data
			})
		},
		setNavBarColor() {
			// navBar-bg-color
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.themeBgColor,
				animation: {
					duration: 400,
					timingFunc: 'easeIn'
				}
			})
		},
		uploadPreviewFile() {
			uni.navigateTo({
				url: '/pages/file/file-upload?yzAppType=1'
			})
		},
		uploadEditFile() {
			uni.navigateTo({
				url: '/pages/file/file-upload?yzAppType=2'
			})
		},
		uploadFormatConvertFile() {
			uni.navigateTo({
				url: '/pages/file/file-upload?yzAppType=3'
			})
		},
		uploadEditFileHttp() {
			const fileUrl = 'http://113.62.127.199:8090/fileUpload/1.docx'
			uni.showLoading({
				title: '正在上传',
				mask: false
			})
			this.$minApi
				.yzEditHttpUploadFile({ fileUrl })
				.then(res => {
					uni.hideLoading()
					uni.showToast({
						title: '上传成功'
					})
					const fileVersionId = res.data.fileVersionId
					const sign = generateSign(globalConfig.yzEditAPPKEY, {
						appId: [globalConfig.yzEditAPPID],
						fileVersionId: [fileVersionId],
						userRight: ['0'], // 模式：0:编辑（默认）1:只读
						userName: ['silianpan'] // 用户名称
					})
					uni.navigateTo({
						url:
							'/pages/yzcloud/yz-edit?url=' +
							encodeURIComponent(
								`${globalConfig.yzEic}/api/edit/file?fileVersionId=${fileVersionId}&appId=${globalConfig.yzEditAPPID}&sign=${sign}&userRight=1&userName=silianpan`
							)
					})
				})
				.catch(err => {
					uni.hideLoading()
					uni.showToast({
						title: '上传失败'
					})
				})
		}
	}
}
</script>
